package com.lagou.homework.service.impl;

import com.lagou.homework.dao.ValidateCodeDao;
import com.lagou.homework.pojo.AuthCode;
import com.lagou.homework.service.ValidateCodeService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;


import java.sql.Timestamp;
import java.util.Date;

@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {

    @Autowired
    ValidateCodeDao validateCodeDao;

    @Override
    public Integer validate(String email, String code) {
        AuthCode authCode = validateCodeDao.findBySql(email);
        String dbCode = authCode.getCode();
        //System.out.println(dbCode);
        // 判断是否超时
        long l = authCode.getExpiretime().getTime();
        Timestamp expiretime = new Timestamp(l - 8*60*60*1000);
        //System.out.println("超时时间：" + expiretime);
        // 获取当前时间 和 expiretime 比较
        boolean before = expiretime.before(new Date());
        //System.out.println("expiretime 是否在 现在的前面：" + before);
        if(before){
            return 2;
        }else if(!dbCode.equals(code)){
            // 判断验证码是否正确
            return 1;
        }else {
            return 0;
        }
    }
}
